<?php

/**
 * Maikuro CMS
 *
 * LICENSE:
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * The license is bundled with this package in the file LICENSE.txt.
 * You may also obtain a copy of the License at this URL:
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @copyright  Copyright (c) 2010, Jonathan Hedrén
 * @author     Jonathan Hedrén <jonathan.hedren@gmail.com>
 * @license    http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
 */

/**
 * @see IObjectFactory
 */
require_once 'di/IObjectFactory.php';

/**
 * @see AbstractDbEntityService
 */
require_once 'service/AbstractDbEntityService.php';

/**
 * @see AdministratorService
 */
require_once 'service/AdministratorService.php';

/**
 * @see PermissionService
 */
require_once 'service/PermissionService.php';

/**
 * @see PageService
 */
require_once 'service/PageService.php';

/**
 * @see PluginService
 */
//require_once 'service/PluginService.php';

/**
 * @see SettingService
 */
require_once 'service/SettingService.php';

/**
 * @see SiteService
 */
require_once 'service/SiteService.php';

/**
 * @see TemplateService
 */
require_once 'service/TemplateService.php';

/**
 * 
 */
class ServiceFactory implements IObjectFactory
{
    /**
     * The administrator service.
     *
     * @var AdministratorService
     */
    private $administratorService;

    /**
     *
     * @var string
     */
    private $basePath;

     /**
     * The cache handler.
     *
     * @var ICacheHandler
     */
    protected $cacheHandler;

    /**
     * The database handler
     *
     * @var ExtendedPDO
     */
    protected $pdo;

    /**
     * The page service for each site stored in an array with the site's id as
     * key.
     *
     * @var array
     */
    private $pageService;

    /**
     * The permission service
     *
     * @var PermissionService
     */
    private $permissionService;

    /**
     * The plugin service
     *
     * @var PluginService
     */
    //private $pluginService;

    /**
     * The setting service
     *
     * @var SettingService
     */
    private $settingService;

    /**
     * The site service
     *
     * @var SiteService
     */
    private $siteService;

    /**
     * The template service
     *
     * @var TemplateService
     */

    private $templateService;

    /**
     *
     * @param ExtendedPDO $pdo
     * @param ICacheHandler $cacheHandler
     * @param string $basePath
     */
    public function __construct(ExtendedPDO $pdo, ICacheHandler $cacheHandler, $basePath)
    {
        $this->pdo = $pdo;
        $this->cacheHandler = $cacheHandler;
        $this->basePath = $basePath;
    }

    /**
     *
     * @return AdministratorService
     */
    public function getAdministratorService()
    {
        if ($this->administratorService === null) {
            $this->administratorService = new AdministratorService($this->cacheHandler, $this->pdo);
        }
        return $this->administratorService;
    }

    /**
     *
     * @return ICacheHandler
     */
    public function getICacheHandler()
    {
        return $this->cacheHandler;
    }

    /**
     *
     * @return PageService
     */
    public function getPageService()
    {
        if (!isset($this->pageService)) {
            $this->pageService = new PageService($this->cacheHandler, $this->pdo);
        }
        return $this->pageService;
    }

    /**
     *
     * @return PermissionService
     */
    public function getPermissionService()
    {
        if ($this->permissionService === null) {
            $this->permissionService = new PermissionService($this->cacheHandler, $this->pdo);
        }
        return $this->permissionService;
    }

    /**
     *
     * @return PluginService
     * /
    public function getPluginService()
    {
        if ($this->pluginService === null) {
            $this->pluginService = new PluginService($this->cacheHandler, $this->pdo);
        }
        return $this->pluginService;
    }
     * 
     */

    /**
     * Returns the setting service.
     *
     * @return SettingService
     */
    public function getSettingService()
    {
        if ($this->settingService === null) {
            $this->settingService = new SettingService($this->cacheHandler, $this->pdo);
        }
        return $this->settingService;
    }

    /**
     * Returns the site service.
     *
     * @return SiteService
     */
    public function getSiteService()
    {
        if ($this->siteService === null) {
            $this->siteService = new SiteService($this->cacheHandler, $this->pdo);
        }
        return $this->siteService;
    }

    /**
     * Returns the template service.
     *
     * @return TemplateService
     */
    public function getTemplateService()
    {
        if ($this->templateService === null) {
            $this->templateService = new TemplateService($this->basePath);
        }
        return $this->templateService;
    }

    /**
     *
     * @param string $className
     * @return mixed
     */
    public function instance($className)
    {
        $method = 'get' . $className;
        return $this->$method();
    }
}